package com.samuel.basics.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.samuel.basics.annotation.CacheLock;
import com.samuel.basics.annotation.OperateLog;
import com.samuel.basics.base.BaseController;
import com.samuel.basics.constant.CommonConstant;
import com.samuel.basics.constant.OperateConstant;
import com.samuel.basics.entity.LoginLogEntity;
import com.samuel.basics.exception.CustomException;
import com.samuel.basics.service.LoginLogService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;


/**
* <p>
* 登录日志-前端控制器
* </p>
*
* @author 黄阿能
* @since 2020-01-16
*/

@Controller
@RequestMapping("/loginLog")
public class LoginLogController extends BaseController {
    
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Resource
    private LoginLogService loginLogService;

    private final static String PREFIX = "log/";

    /**
    * 跳转到登录日志首页
    */
    @GetMapping("/list")
    public String list() {
        return PREFIX + LIST;
    }

    @ResponseBody
    @GetMapping("/listJson")
    @OperateLog(operateModule = "登录日志", operateType = OperateConstant.SELECT, operateDesc = "查看登录日志列表")
    public JSONObject listJson(Page<LoginLogEntity> iPage, LoginLogEntity loginLogEntity) {
        QueryWrapper<LoginLogEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(loginLogEntity.getLoginName())) {
            queryWrapper.lambda().like(LoginLogEntity::getLoginName, loginLogEntity.getLoginName());
        }
        if (StringUtils.isNotBlank(loginLogEntity.getIp())) {
            queryWrapper.lambda().like(LoginLogEntity::getIp, loginLogEntity.getIp());
        }
        queryWrapper.lambda().orderByDesc(LoginLogEntity::getCreateDate);
        iPage = loginLogService.page(iPage, queryWrapper);
        return page2LayUiTablePage(iPage);
    }

    @GetMapping("/view")
    @CacheLock(prefix = "view")
    @ExceptionHandler(CustomException.class)
    @OperateLog(operateModule = "登录日志", operateType = OperateConstant.VIEW, operateDesc = "查看登录日志信息")
    public String view(String id, ModelMap modelMap) {
        LoginLogEntity LoginLogEntity = loginLogService.getById(id);
        modelMap.put("LoginLogEntity", LoginLogEntity);
        return PREFIX + VIEW;
    }


    /**
    * 批量删除
    *
    * @param ids id集合
    */
    @ResponseBody
    @PostMapping("delete")
    @CacheLock(prefix = "loginLog-delete")
    @OperateLog(operateModule = "登录日志", operateType = OperateConstant.DELETE, operateDesc = "删除登录日志，可以批量删除")
    public JSONObject delete(String ids) {
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(ids)) {
            jsonObject.put(CommonConstant.AjaxCode.CODE, CommonConstant.AjaxCode.FAIL_CODE);
            jsonObject.put(CommonConstant.AjaxCode.MESSAGE, "请至少选择一条数据！");
            return jsonObject;
        }
        try {
            loginLogService.removeByIds(Arrays.asList(ids.split(CommonConstant.DOC_FLAG)));
            jsonObject.put(CommonConstant.AjaxCode.CODE, CommonConstant.AjaxCode.SUCCESS_CODE);
            jsonObject.put(CommonConstant.AjaxCode.MESSAGE, "删除登录日志数据成功！");
        } catch (Exception e) {
            logger.error("删除失败！原因： {}", e.getMessage());
            jsonObject.put(CommonConstant.AjaxCode.CODE, CommonConstant.AjaxCode.FAIL_CODE);
            jsonObject.put(CommonConstant.AjaxCode.MESSAGE, "删除登录日志数据失败！");
        }

        return jsonObject;
    }
}
